package com.macrohard.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.service.ActivityService;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Activity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("activity")
@Validated
@RequiredArgsConstructor
@Api(tags = "活动接口")
public class ActivityController {

	private final ActivityService activityService;

	@ApiOperation("分页获取所有活动")
	@GetMapping
	public R get(@NotNull Integer current, @NotNull Integer pageSize, String rName) {
		LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
		if (Objects.nonNull(rName)) {
			wrapper = wrapper.eq(Activity::getrName, rName);
		}
		Page<Activity> page = new Page<>(current, pageSize);
		Page<Activity> list = activityService.listAll(page, wrapper);
		return R.ok("success", list);
	}

	@ApiOperation("根据id获取活动")
	@GetMapping("{id}")
	public R getOne(@PathVariable("id") int id) {
		Activity activity = activityService.getOne(id);
		return R.ok("success", activity);
	}

	@ApiOperation("添加活动")
	@PostMapping
	public R add(@RequestBody Activity activity) {
		return R.wrapEffectResult(activityService.save(activity));
	}

	@ApiOperation("修改活动")
	@PutMapping
	public R modify(@RequestBody Activity activity) {
		return R.wrapEffectResult(activityService.updateById(activity));
	}

	@ApiOperation("根据id删除活动")
	@DeleteMapping("{id}")
	public R remove(@PathVariable("id") int id) {
		return R.wrapEffectResult(activityService.removeById(id));
	}

	@ApiOperation("批量删除活动")
	@DeleteMapping
	public R remove(@RequestBody List<Integer> ids) {
		return R.wrapEffectResult(activityService.removeByIds(ids));
	}
}
